package br.com.fsa.aplication.Database;



import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import br.com.fsa.aplication.Exception.ConectionException;
import br.com.fsa.aplication.Model.bean.Usuario;
import br.com.fsa.aplication.Util.HibernateUtil;
import br.com.fsa.aplication.Util.Util;

public class UsuarioDAO extends DaoGenericoImpl<Usuario> {

	public Usuario findUsuario(String _usuario, String _senha)
			throws ConectionException
	{
		Session session = null;
		try
		{
			session = HibernateUtil.getSession().openSession();
			Transaction transaction = session.beginTransaction();
			transaction.begin();
			Criteria criteria = session.createCriteria(Usuario.class);
			criteria = criteria.add(Restrictions.eq("usuario", _usuario));
			criteria = criteria.add(Restrictions.eq("senha", _senha));
			
			transaction.commit();
			
			return (Usuario) criteria.uniqueResult();
	

		}
		catch (HibernateException e)
		{
			Util.gravaLog(e.getMessage());
			throw new ConectionException(e.getMessage());

		}
		catch (ConectionException e)
		{
			Util.gravaLog(e.getMessage());
			throw e;
		}
		finally
		{
			if( session.isOpen())
			{
				session.close();
			}
			
		}

	}
}
